package good.idea.data.excel.context;

import lombok.Data;
import org.apache.poi.ss.formula.functions.Column;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import good.idea.data.excel.entity.CellEntity;
import good.idea.data.excel.entity.RowEntity;
import good.idea.data.excel.entity.TemplateElement;
import good.idea.data.excel.entity.TemplateTag;
import good.idea.data.utils.LangUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chm
 */
@Data
public class ExcelTemplateContext extends ExcelBaseContext {

    private List<File> templateFiles = new ArrayList<>();

    private XSSFWorkbook workbook;

    private XSSFSheet activitySheet;

    private int activitySheetIndex;

    private List<XSSFSheet> tempSheets;

    private List<RowEntity> activityRowEntities;

    private List<List<RowEntity>> allRowEntitys = new ArrayList<List<RowEntity>>();

    private List<List<CellEntity>> allCellEntitys = new ArrayList<List<CellEntity>>();

    private List<List<Column>> allColumns = new ArrayList<List<Column>>();

    /**
     * 当前活跃
     */
    private TemplateElement activityTemplateElement;

    private List<TemplateElement> templateElements = new ArrayList<>();

    private Map<Integer, List<TemplateTag>> templateTagMap = new LinkedHashMap();

    private Map<Integer, List<RowEntity>> rowEntityMap = new LinkedHashMap();

    private Map<Integer, List<CellEntity>> cellEntityMap = new LinkedHashMap();

    private TemplateTag activeTemplateTag;

    public boolean hasTemplateFile() {
        if (LangUtils.isNotEmpty(this.templateFiles)) {
            for (File file : this.templateFiles) {
                if (!file.exists()) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    public List<RowEntity> getActiveRowEntities() {
        List<RowEntity> items = rowEntityMap.get(this.getActivitySheetIndex());
        if (items == null) {
            items = new ArrayList<>();
            rowEntityMap.put(this.getActivitySheetIndex(), items);
        }
        return items;
    }

    public List<CellEntity> getActiveCellEntities() {
        List<CellEntity> items = cellEntityMap.get(this.getActivitySheetIndex());
        if (items == null) {
            items = new ArrayList<>();
            cellEntityMap.put(this.getActivitySheetIndex(), items);
        }
        return items;
    }

    public List<TemplateTag> getActiveTemplateTags() {
        List<TemplateTag> items = templateTagMap.get(this.getActivitySheetIndex());
        if (items == null) {
            items = new ArrayList<>();
            templateTagMap.put(this.getActivitySheetIndex(), items);
        }
        return items;
    }

    public List<RowEntity> getActivityRowEntities(int index) {
        return this.allRowEntitys.get(index);
    }

    public TemplateElement getActivityTemplateElement(int index) {
        return templateElements.get(index);
    }


}
